package com.meichis.yndms.ui.common;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;

import androidx.fragment.app.Fragment;

import com.meichis.mcsappframework.dialog.LoadingDialog;
import com.meichis.mcsappframework.utils.ActivityUtils;
import com.meichis.mcsappframework.utils.MessageTools;
import com.meichis.mcsappframework.utils.SharePreferenceUtil;
import com.meichis.yndms.R;

/**
 * Created by chenhaifei on 2016/7/14.
 */
public abstract class BaseFragment extends Fragment {
    protected SharePreferenceUtil util;
    protected String AuthKey = "";

    private View mView;
    private LoadingDialog dialog_loading;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        util = SharePreferenceUtil.getInstance();
        AuthKey = (String) util.getCacheValue(SharePreferenceUtil.PREFERENCES_AUTHKEY);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = onCreateDataBindingView(inflater,container,savedInstanceState);
        init();
        return mView;
    }
    protected abstract View onCreateDataBindingView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);

    protected abstract void init();

    public void showLoading(int strRec) {
        showLoading(getResources().getString(strRec), false);
    }

    public void showLoading(boolean showLoading) {
        if(showLoading){
            showLoading(getResources().getString(R.string.loading), true);
        }else {
            hideLoading();
        }

    }

    public void showLoading(String progressMsg, boolean canCancel) {
        if (getActivity().isFinishing()) return;
        if (dialog_loading == null) {
            dialog_loading = new LoadingDialog(getActivity());
        }
        if (!dialog_loading.isShowing()) {
            dialog_loading.show();
        }
        dialog_loading.setText(progressMsg);
        dialog_loading.setCancelable(canCancel);
    }

    public void hideLoading() {
        if (dialog_loading != null) {
            dialog_loading.dismiss();
        }
    }

    public void showError(String ErrorMsg) {
        MessageTools.showLongToast(ErrorMsg);
    }

    public void showMsg(String Msg) {
        MessageTools.showLongToast(Msg);
    }

    public void openActivity(Class<?> pClass) {
        openActivity(pClass, null);
    }

    public void openActivity(Class<?> pClass, Bundle pBundle) {
        Intent intent = new Intent(getActivity(), pClass);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }
        startActivity(intent);
    }

    public void openActivity(Class<?> pClass, int flag) {
        Intent intent = new Intent(getActivity(), pClass);
        intent.setFlags(flag);
        startActivity(intent);
    }

    public void openIntentActivity(Class<?> pClass, Intent intent) {
        intent.setClass(getActivity(), pClass);
        startActivity(intent);
    }

    public void openActivityAndClose(Class<?> pClass) {
        openActivity(pClass, null);
        ActivityUtils.getInstance().FinishActivity(getActivity().getLocalClassName());
    }

    public void openActivityAndClose(Class<?> pClass, Bundle pBundle) {
        openActivity(pClass, pBundle);
        ActivityUtils.getInstance().FinishActivity(getActivity().getLocalClassName());
    }

    public void close() {
        ActivityUtils.getInstance().FinishActivity(getActivity().getLocalClassName());
    }

    public void hideKeyboard() {
        try {
            ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        } catch (Exception e) {
        }
    }

    public void ShowKeyBoard() {
        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }
}
